<template>
  <div>
    <el-upload
      :class="{ disabled: fileComputed }"
      list-type="picture-card"
      :file-list="fileList"
      action="#"
      :limit="1"
      :on-remove="handleRemove"
      :on-change="changeFile"
      :on-preview="preview"
      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-dialog :visible.sync="showDialog" title="图片">
      <img :src="imgUrl" alt="" style="width: 100%">
    </el-dialog>
    <el-progress v-if="showPrecent" style="width: 180px" :percentage="percent" />
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
var cos = new COS({
  // 需要把id和key改成自己的信息 具体查看https://console.cloud.tencent.com/cam/capi
  SecretId: 'AKIDSwqhUI4vmV7buVD1uOoFofO1WItGND33', // 身份识别 ID
  SecretKey: 'zmPp7Ncm6MfyJSP9i6Ofgq2Aa5W3Tp5F' // 身份密钥
})
export default {
  data() {
    return {
      fileList: [], // 图片地址设置为数组
      showDialog: false, // 控制显示弹层
      imgUrl: '',
      currentFileUid: '', // 当前上传文件id
      showPrecent: false, // 控制进度条显示
      percent: 0 // 进度条程度
    }
  },
  computed: {
    // 计算属性 判断是否已经上传完了一张
    fileComputed() {
      return this.fileList.length === 1
    }
  },
  methods: {
    preview(file) {
      this.imgUrl = file.url
      this.showDialog = true
    },
    handleRemove(file) {
      this.fileList = []
    },
    changeFile(file, fileList) {
      console.log(fileList)
      fileList.forEach(item => {
        if (!this.fileList.find(file => file.uid === item.uid)) {
          this.fileList.push(item)
        }
      })
      console.log(this.fileList, 'fil')
    },
    beforeUpload(file) {
      // 检测文件类型
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!')
        return false
      }
      // 检测大小
      const maxSize = 5 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.error('图片大小最大不能超过5M')
        return false
      }
      this.showPrecent = true
      this.currentFileUid = file.uid
      return true
    },
    // 覆盖默认上传行为
    upload(params) {
      console.log(params, 'params')
      cos.putObject({
        Bucket: '107w-hrsaas-1307580524',
        Region: 'ap-beijing',
        Key: params.file.name, // 文件名
        Body: params.file, // 要上传的文件对象
        StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
        onProgress: (params) => {
          this.percent = params.percent * 100
        }
      }, (err, data) => {
        console.log(data, err, '1111111')
        if (!err && data.statusCode === 200) {
          setTimeout(() => {
            this.showPrecent = false
          }, 1000)
          this.fileList = this.fileList.map(item => {
            // 去找谁的uid等于刚刚记录下来的id
            // 就把url改成腾讯云服务器上的图片地址
            if (item.uid === this.currentFileUid) {
              return {
                url: 'http://' + data.Location,
                upload: true
              }
            }
            return item
          })
        }
      })
    }
  }
}
</script>

<style>
.disabled .el-upload--picture-card {
  display: none;
}
</style>

